#include "rtl_private_op.h"

int rtl_chip_base_init(void)
{
  rtk_api_ret_t rtk_ret;
  rtk_ret = rtl8373_setAsicRegBits(
      RTL8373_RST_GLB_CTRL_0_ADDR,              // 0x24
      RTL8373_RST_GLB_CTRL_0_SW_CHIP_RST_MASK,  // 1
      RTL8373_RST_GLB_CTRL_0_SW_CHIP_RST_OFFSET // 0
  );
  if (RT_ERR_OK != rtk_ret)
  {
    LOG_ERROR("reboot rtl8372 fail!\n");
    return -1;
  }
  else
  {
    LOG_INFO("reboot rtl8372 success!\n");
  }

  DELAY_MS(1000);
  if (RT_ERR_OK != rtk_switch_init())
  {
    LOG_ERROR("init rtk switch fail!\n");
    return -1;
  }
  else
  {
    LOG_INFO("init rtk switch success!\n");
  }
  return 0;
}
int sfp_is_detect_present(void)
{
  rtk_gpio_level_t pin_value;
  if (RT_ERR_OK != rtk_gpio_pinVal_read(37, &pin_value))
  {
    // LOG_ERROR("read gpio37 value fail!\n");
    return -1;
  }
  else
  {
    // LOG_INFO("read gpio37 value success!\n");
  }
  return pin_value ^ 1;
}
int rtl_get_sfp_type(rtk_sds_mode_t *pSdsMode)
{
  uint32_t i2c_read_data;
  if (RT_ERR_OK != rtk_i2c_data_read(0x0C, 1, 1, &i2c_read_data))
  {
    LOG_ERROR("read i2c data fail!\n");
    return -1;
  }
  else
  {
    LOG_INFO("read i2c data success!\n");
  }
  if ((99 < i2c_read_data) && (i2c_read_data < 104)) // 103
  {
    *pSdsMode = SERDES_10GR;
  }
  else if ((30 < i2c_read_data) && (i2c_read_data < 33)) // 31
  {
    *pSdsMode = SERDES_2500BASEX;
  }
  else if ((10 < i2c_read_data) && (i2c_read_data < 15)) // 13
  {
    *pSdsMode = SERDES_1000BASEX;
  }
  else if ((0 < i2c_read_data) && (i2c_read_data < 3)) // 1
  {
    *pSdsMode = SERDES_100FX;
  }
  else
  {
    *pSdsMode = SERDES_10GR;
    return -1;
  }
  return 0;
}

int rtl_check_sfp_interface_init(void)
{
  if (RT_ERR_OK != rtk_gpio_pinDir_set(37, GPIO_DIR_INPUT))
  {
    LOG_ERROR("set gpio37 dir fail!\n");
    return -1;
  }
  if (RT_ERR_OK != rtk_i2c_init(I2C_SCL_CLK_50KHZ, 0x50))
  {
    LOG_ERROR("init i2c fail!\n");
    return -1;
  }
  if (RT_ERR_OK != rtk_i2c_readMode_set(I2C_READ_STANDARD_MODE))
  {
    LOG_ERROR("set i2c read mode fail!\n");
    return -1;
  }
  if (RT_ERR_OK != rtk_i2c_gpioPinGroup_set(3, 3))
  {
    LOG_ERROR("set i2c gpio pin group fail!\n");
    return -1;
  }
  return 0;
}
int rtl_chip_sfp_check(uint8_t *on_off_line, rtk_sds_mode_t *pSdsMode)
{
  if (rtl_check_sfp_interface_init() != 0)
  {
    LOG_ERROR("check sfp interface init fail!\n");
    return -1;
  }
  *on_off_line = 0;
  *pSdsMode = SERDES_10GR;
  if (sfp_is_detect_present() == 1)
  {
    *on_off_line = 1;
    if (0 == rtl_get_sfp_type(pSdsMode))
    {
      return 0;
    }
    return 2;
  }
  return 0;
}

int rtl_set_sfp_status(rtk_sds_mode_t pSdsMode)
{
  rtk_api_ret_t rtk_ret;
  if (pSdsMode == SERDES_10GR)
  {
    /**
     * @brief #SDS0RX PN SWAP FOR 10G
     *
     */
    rtk_ret = dal_rtl8373_sds_regbits_write(0, 6, 2, 0x2000, 1);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }
    /**
     * @brief#SDS0TX PN  for 10G 不交换，为了这个问题，白扯了一天，1，交换，0 不交换
     *
     */
    rtk_ret = dal_rtl8373_sds_regbits_write(0, 6, 2, 0x4000, 0);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }

    rtk_ret = rtl8373_setAsicRegBits(RTL8373_SMI_MAC_TYPE_CTRL_ADDR,
                                     RTL8373_SMI_MAC_TYPE_CTRL_MAC_PORT3_TYPE_MASK,
                                     0);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }
    rtk_ret = rtk_sdsMode_set(0, SERDES_10GR);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }
  }
  else
  {
    /**
     * @brief #SDS0RX PN SWAP FOR 10G
     *
     */
    rtk_ret = dal_rtl8373_sds_regbits_write(0, 0, 0, 0x200, 1);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }
    /**
     * @brief#SDS0TX PN  for 10G 不交换，为了这个问题，白扯了一天，1，交换，0 不交换
     *
     */
    rtk_ret = dal_rtl8373_sds_regbits_write(0, 0, 0, 0x100, 0);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }

    rtk_ret = rtl8373_setAsicRegBits(RTL8373_SMI_MAC_TYPE_CTRL_ADDR,
                                     RTL8373_SMI_MAC_TYPE_CTRL_MAC_PORT3_TYPE_MASK,
                                     0);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }
    rtk_ret = rtk_sdsMode_set(0, pSdsMode);
    if (rtk_ret != RT_ERR_OK)
    {
      return -1;
    }
  }
  return 0;
}
void rtl_sfp_module_check_and_set_cycle(void)
{
  rtk_sds_mode_t pSdsMode;
  if (setting_para->sfp_auto_flag == 1)
  {
    if (sfp_is_detect_present())
    {
      if (g_sfp_setting_status != SFP_STATUS_HAS_SET) //设置过
      {
        LOG_INFO("sfp setting excute!\n");
        if (0 == rtl_get_sfp_type(&pSdsMode))
        {
          if (g_pSdsMode != pSdsMode)
          {
            g_pSdsMode = pSdsMode;
            LOG_INFO("set sfp type %d", g_pSdsMode);
            rtl_set_sfp_status(g_pSdsMode);
          }
          g_sfp_setting_status = SFP_STATUS_HAS_SET;
        }
        else
        {
          g_sfp_setting_status = SFP_STATUS_HAS_SET;
          BITSET(ERROR_CODE, SFP_ERROR);
          LOG_ERROR("sfp type read error!\n");
        }
      }
    }
    else
    {
      g_sfp_setting_status = SFP_STATUS_REMOVE;
    }
  }
  else
  {
    if (g_sfp_setting_status == SFP_STATUS_NEED_SET) //没设置过
    {
      LOG_INFO("sfp setting force set!\n");
      LOG_INFO("set sfp type %d", setting_para->sfp_type);
      rtl_set_sfp_status((rtk_sds_mode_t)setting_para->sfp_type);
      g_sfp_setting_status = SFP_STATUS_HAS_SET;
    }
  }
}

void rtl_private_set_vlan_cycle(void)
{
  if (g_vlan_set_status == VLAN_NEED_SET)
  {
    if (RT_ERR_OK != rtk_vlan_init())
    {
      LOG_ERROR("init rtk vlan fail!\n");
    }
    else
    {
      LOG_INFO("init rtk vlan success!\n");
    }
    LOG_INFO("set vlan id:%d!\n", setting_para->VLAN_TABLE[0]);
    rtl_private_set_run_vlan(setting_para->VLAN_TABLE[0]);
    g_vlan_set_status = VLAN_HAS_SET;
  }
}

int rtl_private_set_run_vlan(rtk_uint32 vlan_id)
{
  rtk_vlan_entry_t vlan1300;
  volatile rtk_api_ret_t rtk_ret;
  memset(&vlan1300, 0x00, sizeof(rtk_vlan_entry_t));
  RTK_PORTMASK_PORT_SET(vlan1300.mbr, UTP_PORT3); /* 加入VLAN 1300成员集 */
  RTK_PORTMASK_PORT_SET(vlan1300.mbr, UTP_PORT4);
  RTK_PORTMASK_PORT_SET(vlan1300.mbr, UTP_PORT5);
  RTK_PORTMASK_PORT_SET(vlan1300.mbr, UTP_PORT6);
  RTK_PORTMASK_PORT_SET(vlan1300.mbr, UTP_PORT7);

  RTK_PORTMASK_PORT_SET(vlan1300.untag, UTP_PORT4); /* （ACCESS模式核心） */
  RTK_PORTMASK_PORT_SET(vlan1300.untag, UTP_PORT5);
  RTK_PORTMASK_PORT_SET(vlan1300.untag, UTP_PORT6);
  RTK_PORTMASK_PORT_SET(vlan1300.untag, UTP_PORT7);

  rtk_ret = rtk_vlan_set(vlan_id, &vlan1300); /* 将配置应用到VLAN 1300 */

  /* 2. 设PVID为VLAN 1300（入帧打VLAN 1300标签） */
  //rtk_vlan_portPvid_set(UTP_PORT3, 1300);
  rtk_ret = rtk_vlan_portPvid_set(UTP_PORT4, vlan_id);
  rtk_ret = rtk_vlan_portPvid_set(UTP_PORT5, vlan_id);
  rtk_ret = rtk_vlan_portPvid_set(UTP_PORT6, vlan_id);
  rtk_ret = rtk_vlan_portPvid_set(UTP_PORT7, vlan_id);

  rtk_vlan_portAcceptFrameType_set(UTP_PORT4, ACCEPT_FRAME_TYPE_UNTAG_ONLY);
  rtk_vlan_portAcceptFrameType_set(UTP_PORT5, ACCEPT_FRAME_TYPE_UNTAG_ONLY);
  rtk_vlan_portAcceptFrameType_set(UTP_PORT6, ACCEPT_FRAME_TYPE_UNTAG_ONLY);
  rtk_vlan_portAcceptFrameType_set(UTP_PORT7, ACCEPT_FRAME_TYPE_UNTAG_ONLY);
  /*光口仅接受tagged帧（拒绝untagged帧，符合TRUNK模式） */
  rtk_vlan_portAcceptFrameType_set(UTP_PORT3, ACCEPT_FRAME_TYPE_TAG_ONLY);
  return 0;
}
